$(function () {
		function loadData() {
				var urladr = window.location.href;
				var bookname = urladr.split("/")[urladr.split("/").length - 1];
				loadbookmsg(bookname);
				loadbookcatlog(bookname);
				editorpush();
		}

		//加载小说信息
		function loadbookmsg(bookname) {
				$.post("/wfRead/novel/loadbookmsg",
						"bookname=" + bookname, function (data) {

								if (data.flag) {
										var novel = data.data;

										var name = novel.bookName;
										$(".msg_right h2 dt a").text("《" + name + "》");
										$(".box_header a").text("【" + name + "】");

										var types = novel.bookType;
										$(".msg_right dd a").eq(0).text("");
										$("#Mian_left_head h2").text(types[0].length > 2 ? types[1] : types[0]);
										for (var i = 0; i < 2; i++) {
												var temp = $(".msg_right dd a").eq(0).text();
												$(".msg_right dd a").eq(0).text(temp + " " + types[i]);
										}
										//console.log("types="+$(".msg_right dd a").eq(0).text())
										getDownloadRank($(".msg_right dd a").eq(0).text());
										var author = novel.bookAuthor;
										$(".msg_right dd a").eq(1).text(author);
										loadauthwork();
										var state = novel.bookState;
										$(".msg_right dd a").eq(2).text(state);

										var intro = novel.bookIntro;
										$(".bookintro p").html("<strong>" + name + ":</strong>" + intro);

										var words = novel.bookWords;
										if (words / 10000 > 0)
												$(".msg_right dd a").eq(3).text(words / 10000 + "万");
										else
												$(".msg_right dd a").eq(3).text(words);

										var updatetime = novel.updateTime;
										var myDate = new Date(updatetime);
										var year = myDate.getFullYear(); //获取当前年
										var mon = myDate.getMonth() + 1; //获取当前月
										var date = myDate.getDate(); //获取当前日
										var updatetime = year + "-" + mon + "-" + date;
										$(".msg_right dd a").eq(4).text(updatetime);
										console.log(escape(name).replaceAll("%", "_").toLowerCase())
										var imgurl = "/wfRead/picture/" + escape(name).replaceAll("%", "_").toLowerCase() + ".jpg";
										$(".msg_left img")[0].src = imgurl;

										$("#download")[0].href = "/wfRead/novel/download?novelname=" + name + ".txt";
								}
						}, "json");
				//loadauthwork();
		}

		//加载小说目录
		function loadbookcatlog(bookname) {
				$.post("/wfRead/novel/loadbookcatlog",
						"bookname=" + bookname, function (data) {
								var catalogs = data.data;
								//清空ul中的数据
								$("#bookmenu_list ul").empty();
								$(".newlist10 ul").empty();
								//将目录加载
								if (catalogs.length==0){
										console.log(catalogs)
										return;
								}
								var read = $("#read")[0].href = "/wfRead/read/" + bookname + "/" + catalogs[0].novelCatalog;
								for (var i = 0; i < catalogs.length; i++) {
										//console.log(catalogs[i])
										var time = new Date(catalogs[i].updateTime);
										time = time.getFullYear() + "-" + time.getMonth() + "-" + time.getDay();
										if (i < 20) {
												//var li = "<li><a href=\"/wfRead/read/" + bookname + "/" + catalogs[i].novelCatalog + "\" target=\"_blank\">" + catalogs[i].novelCatalog + "-" + time + "</a>\n" ;
												var li = "<li><a href=\"/wfRead/read/" + bookname + "/" + catalogs[i].novelCatalog + "\" target=\"_blank\">" + catalogs[i].novelCatalog + "</a>\n" ;
												if (catalogs[i].vip == 1) {
														li += "<span id='vip'>vip</span>";
												}
												li += "</li>";
												$("#bookmenu_list ul").append(li);
										} else {
												//var li = "<li style='display: none'><a href=\"/wfRead/read/" + bookname + "/" + catalogs[i].novelCatalog + "\" target=\"_blank\">" + catalogs[i].novelCatalog + "-" + time + "</a>\n";
												var li = "<li style='display: none'><a href=\"/wfRead/read/" + bookname + "/" + catalogs[i].novelCatalog + "\" target=\"_blank\">" + catalogs[i].novelCatalog  + "</a>\n";
												if (catalogs[i].vip == 1) {
														li += "<span id='vip'>vip</span>";
												}
												li += "</li>";
												$("#bookmenu_list ul").append(li);
										}

								}
								//加载最新10章
								for (var i = catalogs.length - 1; i > catalogs.length - 11; i--) {
										if (i < 0) {
												break;
										}
										var time = new Date(catalogs[i].updateTime);
										time = time.getFullYear() + "-" + time.getMonth() + "-" + time.getDay();
										//var li = "<li><a href=\"/wfRead/read/" + bookname + "/" + catalogs[i].novelCatalog + "\" target=\"_blank\">" + catalogs[i].novelCatalog + "-" + time + "</a>\n";
										var li = "<li><a href=\"/wfRead/read/" + bookname + "/" + catalogs[i].novelCatalog + "\" target=\"_blank\">" + catalogs[i].novelCatalog + "</a>\n";
										if (catalogs[i].vip == 1) {
												li += " <span id='vip'>vip</span></li>";
										} else {
												li += "</li>";
										}
										$(".newlist10 ul").append(li);
								}
						}, "json")
		}

		//显示更多--加载
		function showMore() {
				$("#bookmenu_list ul li:hidden").attr("style", "display:block");
		}

		//加载作者其他作品
		function loadauthwork() {
				var authname = $(".msg_right dd a").eq(1).text();

				$.post("/wfRead/auth/getauthorworks",
						"authname=" + authname, function (data) {
								if (data.flag) {
										var auth = data.data;
										var works = auth.works;

										$("#page").empty();
										for (var i = 0; i < works.length; i++) {
												//console.log("work=" + works[i].bookWords)
												var words = works[i].bookWords;
												if (words == null) {
														words = 0;
												}
												if (words / 10000 > 0)
														words = works[i].bookWords / 10000 + "万";
												var updatetime = works[i].updateTime;
												var myDate = new Date(updatetime);
												var year = myDate.getFullYear(); //获取当前年
												var mon = myDate.getMonth() + 1; //获取当前月
												var date = myDate.getDate(); //获取当前日
												var updatetime = year + "-" + mon + "-" + date;

												var str = "<ul class=\"zznm\">\n" +
														"                            <li class=\"zzn1\"><a href=\"" + "/wfRead/novel/book/" + works[i].bookName + "\" target=\"_blank\">《" + works[i].bookName + "》全集下载</a></li>\n" +
														"                            <li class=\"zzn2\">字数：" + words + "</li>\n" +
														"                            <li class=\"zzn3\">更新：" + updatetime + "</li>\n" +
														"                        </ul>";

												$("#page").append(str);
										}
								}
						}, "json")
		}

		//点击显示/隐藏作品集
		function showBtn() {
				if ($("#page").css("display") == "block")
						$("#page").attr("style", "display:none");
				else if ($("#page").css("display") == "none")
						$("#page").attr("style", "display:block");
		}

		//获取下载榜
		function getDownloadRank(type) {

				var types = type.split(" ");
				for (var i = 0; i <types.length ; i++) {
						if (types[i].length==2){
								type = types[i];
						}
				}
				//console.log(type)
				$.post("/wfRead/novel/downloadrank",
						"type=" + type + "&size=" + 10,
						function (data) {
								if (data.flag) {
										var booklist = data.data;
										var list = $(".rank_list_ul a");
										//console.log("size="+booklist.length)
										for (var i = 0; i < 10; i++) {
												var bookanme = "《" + booklist[i].bookName + "》全集";
												var url = "/wfRead/novel/book/" + booklist[i].bookName;
												list.eq(i).text(bookanme);
												list[i].href = url;
										}
								}
						}, "json");
		}

		//获取编辑推荐
		function editorpush() {
				var now = new Date();
				var colum = now.getDate();
				$.post("/wfRead/novel/editorpush",
						"colum=" + colum + "&index=" + 10,
						function (data) {
								if (data.flag) {
										var booklist = data.data;
										var list = $(".rank_list_ul a");
										//console.log("size=" + booklist.length)
										for (var i = 0; i < 10; i++) {
												//console.log("book=" + booklist[i])
												var bookanme = "《" + booklist[i].bookName + "》全集";
												var url = "/wfRead/novel/book/" + booklist[i].bookName;
												list.eq(i + 10).text(bookanme);
												list[i + 10].href = url;
										}
								}
						}, "json");
		}

		loadData();
		$("#showlist").click(showBtn);
		$(".more").click(showMore);
})